package cn.tedu.store.product.webapi.repository.impl;

import cn.tedu.store.commons.pojo.product.vo.BrandListItemVO;
import cn.tedu.store.commons.pojo.product.vo.BrandStandardVO;
import cn.tedu.store.product.webapi.repository.IBrandRedisRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Slf4j
@Repository
public class BrandRedisRepositoryImpl implements IBrandRedisRepository {

    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;

    @Override
    public void save(BrandStandardVO brandStandardVO) {
        log.debug("向Redis中写入品牌 参数:{}", brandStandardVO);
        String key = getItemKey(brandStandardVO.getId());
        redisTemplate.opsForValue().set(key, brandStandardVO);
        redisTemplate.opsForSet().add(getAllKeysKey(), key);
        log.debug("写入完成!");
    }

    @Override
    public void save(List<BrandListItemVO> brands) {
        long count = 0;
        log.debug("向Redis中写入品牌列表 品牌数量:{}", brands.size());
        String key = getListKey();
        ListOperations<String, Serializable> ops = redisTemplate.opsForList();
        for (BrandListItemVO brand : brands) {
            ops.rightPush(key, brand);
            ++count;
        }
        redisTemplate.opsForSet().add(getAllKeysKey(), key);
        log.debug("写入列表完成,列表中共有{}条数据", count);
    }

    @Override
    public BrandStandardVO getStandardById(Long id) {
        log.debug("从Redis中读取品牌信息 参数:{}", id);
        Serializable result = redisTemplate.opsForValue().get(getItemKey(id));
        System.out.println(result);
        if (result instanceof BrandStandardVO) {
            return (BrandStandardVO) result;
        }
        return null;
    }

    @Override
    public List<BrandListItemVO> list() {
        return list(0L, -1L);
    }

    @Override
    public List<BrandListItemVO> list(Long start, Long end) {
        log.debug("从Redis中读取品牌列表 起始下标:[{}] 结束下表[{}]", start, end);
        String key = getListKey();
        List<Serializable> resultList = redisTemplate.opsForList().range(key, start, end);
        List<BrandListItemVO> list = new ArrayList<>();
        for (Serializable serializable : resultList) {
            list.add((BrandListItemVO) serializable);
        }
        return list;
    }

    @Override
    public void deleteAll() {
        log.debug("清除所有品牌数据");
        redisTemplate.delete(getAllKeys());
        log.debug("清除完成!");
    }

    private String getItemKey(Long id) {
        return BRAND_ITEM_KEY_PREFIX + id;
    }

    private String getListKey() {
        return BRAND_LIST_KEY;
    }

    private String getAllKeysKey() {
        return BRAND_ALL_KEYS_KEY;
    }

    private Set<String> getAllKeys() {
        Set<Serializable> members = redisTemplate.opsForSet().members(getAllKeysKey());
        Set<String> keys = new HashSet<>();
        for (Serializable member : members) {
            keys.add((String) member);
        }
        return keys;
    }
}
